package com.wyzz.hr.io.extend;

import java.io.*;
import java.util.*;

import com.wyzz.hr.pojo.Inmatch;
import com.wyzz.hr.util.input.StringToObject;

import jxl.*;

public class ReadExcelByImList extends IReadFileByImList {

	public ReadExcelByImList(File inputFile, List imList, int hasTitle,
			IOMessages ioMessages) {
		super(inputFile, imList, hasTitle, ioMessages);
	}

	public List getContent() throws Exception {
		openFile();
		List result = readContent();
		closeFile();
		return result;
	}

	protected List readContent() {
		Sheet sheet = workbook.getSheet(0);
		int colNum = sheet.getColumns();
		int rowNum = sheet.getRows();
		if (colNum == 0 || rowNum == 0) {
			ioMessages.addMessage(new IOMessageSingle(
					"\u6587\u4EF6\u5185\u5BB9\u4E3A\u7A7A",
					Integer.valueOf(-1), new Integer[0]));
			return null;
		}
		if (hasTitle == 0) {
			if (colNum != imList.size()) {
				ioMessages
						.addMessage(new IOMessageSingle(
								"\u5BFC\u5165\u6587\u4EF6\u5FC5\u987B\u548C\u9009\u62E9\u7684\u6A21\u677F\u5217\u6570\u4E00\u81F4",
								Integer.valueOf(-1), new Integer[0]));
				return null;
			}
			int i = 0;
			Inmatch imTmp;
			for (Iterator i$ = imList.iterator(); i$.hasNext(); imTmp
					.setImSortId(Integer.valueOf(i++)))
				imTmp = (Inmatch) i$.next();

		} else {
			Map titleInmatchNumMap = new HashMap();
			Inmatch imTmp;
			for (Iterator it = imList.iterator(); it.hasNext(); titleInmatchNumMap
					.put(imTmp.getImFieldDesc(), imTmp)) {
				imTmp = (Inmatch) it.next();
				imTmp.setImSortId(null);
			}

			for (int i = colNum - 1; i >= 0; i--) {
				String title = sheet.getCell(i, 0).getContents().trim();
				if (titleInmatchNumMap.containsKey(title)) {
					((Inmatch) titleInmatchNumMap.get(title))
							.setImSortId(Integer.valueOf(i));
					titleInmatchNumMap.remove(title);
					continue;
				}
				title = title.split("-")[0].trim();
				if (titleInmatchNumMap.containsKey(title)) {
					((Inmatch) titleInmatchNumMap.get(title))
							.setImSortId(Integer.valueOf(i));
					titleInmatchNumMap.remove(title);
				}
			}

			for (int i = imList.size() - 1; i >= 0; i--) {
				Inmatch inmatch = (Inmatch) imList.get(i);
				if (inmatch.getImSortId() != null)
					continue;
				if (inmatch.getImRequired().intValue() == 1)
					ioMessages
							.addMessage(new IOMessageSingle(
									(new StringBuilder())
											.append(
													"\u5BFC\u5165\u6587\u4EF6\u5FC5\u987B\u542B\u6709")
											.append(inmatch.getImFieldDesc())
											.toString(), Integer.valueOf(-1),
									new Integer[0]));
				imList.remove(i);
			}

		}
		if (ioMessages.hasErrorMsg())
			return null;
		StringToObject stringToObject = new StringToObject();
		List result = new ArrayList();
		List colList;
		for (Iterator i$ = imList.iterator(); i$.hasNext(); result.add(colList)) {
			Inmatch imTmp = (Inmatch) i$.next();
			colList = new ArrayList();
			stringToObject.setFormat(imTmp.getImImb().getImbDataType(), imTmp
					.getImFormat(), new int[] { imTmp.getImDetectError()
					.intValue() });
			for (int i = hasTitle; i < rowNum; i++) {
				Cell cell = sheet.getCell(imTmp.getImSortId().intValue(), i);
				String strTmp = cell.getContents().trim();
				if (strTmp.length() < 1) {
					if (imTmp.getImRequired().intValue() == 1)
						ioMessages.addMessage(new IOMessageSingle(
								(new StringBuilder()).append(
										imTmp.getImFieldDesc()).append(
										"\u5FC5\u987B\u586B\u5199").toString(),
								Integer.valueOf(-1), new Integer[] {
										Integer.valueOf(i + 1),
										imTmp.getImSortId() }));
					else
						colList.add(null);
					continue;
				}
				boolean judgeError = true;
				Object objTmp = null;
				if (stringToObject.getDataType().equalsIgnoreCase("date")) {
					if (cell.getType() == CellType.DATE) {
						objTmp = new Date(((DateCell) cell).getDate().getTime());
						judgeError = false;
					} else {
						objTmp = (Date) stringToObject.toObject(strTmp);
					}
				} else {
					objTmp = stringToObject.toObject(strTmp);
				}
				if (objTmp == null && imTmp.getImRequired().intValue() == 1)
					ioMessages.addMessage(new IOMessageSingle(
							(new StringBuilder())
									.append(imTmp.getImFieldDesc()).append(
											"\u683C\u5F0F\u4E0D\u6B63\u786E")
									.toString(), Integer.valueOf(-1),
							new Integer[] { Integer.valueOf(i + 1),
									imTmp.getImSortId() }));
				else
					colList.add(objTmp);
				if (judgeError && stringToObject.getResultType() < 0) {
					ioMessages.addMessage(new IOMessageSingle(
							(new StringBuilder())
									.append(imTmp.getImFieldDesc()).append(
											"\u683C\u5F0F\u4E0D\u6B63\u786E")
									.toString(), Integer.valueOf(-1),
							new Integer[] { Integer.valueOf(i + 1),
									imTmp.getImSortId() }));
					continue;
				}
				if (judgeError && stringToObject.getResultType() > 0) {
					System.out.println((new StringBuilder()).append("=====")
							.append(cell.getContents()).toString());
					ioMessages
							.addMessage(new IOMessageSingle(
									(new StringBuilder())
											.append(imTmp.getImFieldDesc())
											.append(
													"\u683C\u5F0F\u4E0D\u6B63\u786E\uFF0C\u5DF2\u7ECF\u5FFD\u7565\u6216\u88AB\u5F3A\u5236\u8F6C\u6362")
											.toString(), Integer.valueOf(1),
									new Integer[] { Integer.valueOf(i + 1),
											imTmp.getImSortId() }));
				}
			}

		}

		return result;
	}

	protected void openFile() throws Exception {
		inputStream = new FileInputStream(inputFile);
		workbook = Workbook.getWorkbook(inputStream);
	}

	protected void closeFile() throws Exception {
		workbook.close();
		workbook = null;
		inputStream.close();
		inputStream = null;
	}

	private Workbook workbook;
}
